<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>礼包领取</title>
    <link rel="stylesheet" href="../../icon/iconfont.css">
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/style.css" />
    <link rel="stylesheet" href="../../css/aui.css">
    <link rel="stylesheet" href="../../css/frame0/gift-bag-lingqu.css">

    <style>

    </style>
</head>

<body>
  <div class="giftBag">
    <!-- 头部 -->
    <header>
      <span class="iconfont icon-fanhui" onClick="api.closeWin()"></span>
      <span>礼包</span>
    </header>
    <!-- 卡券区域 -->
    <div class="content" id="content">
      <!-- <div class="couponList">
        <div class="couponImg">
          <img src="../../image/5555.jpg" alt="">
        </div>
        <div class="couponCenter">
          <p>寿县美好时光</p>
          <p>20元餐饮优惠券</p>
          <p>有效期：截止2020.12.31</p>
        </div>
        <div class="couponBtn">
          <button class="btn_conversion">领取</button>
        </div>
      </div> -->
    </div>
</body>
<script type="text/javascript" src="../../lib/zepto.js"></script>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/dns.js"></script>
<script type="text/javascript" src="../../script/aui-toast.js"></script>
<script type="text/javascript" src="../../lib/artTemplate/template.js"></script>
<script type="text/html" id="getbag">
  {{each items as value index}}
    <div class="couponList">
      <div class="couponImg">
        <img src="{{value.merchant.img.url}}" alt="">
      </div>
      <div class="couponCenter">
        <p>{{value.merchant.title}}</p>
        <p>{{value.title}}</p>
        {{if value.limit_time_type==1}}
          <p>有效期：{{value.day.str}}</p>
        {{/if}}
        {{if value.limit_time_type==2}}
          <p>有效期：{{value.start_time}}至{{value.end_time}}</p>
        {{/if}}
      </div>
      <div class="couponBtn">
        <button class="btn_conversion" couponId="{{value.id}}">领取</button>
      </div>
    </div>
  {{/each}}
</script>
<script>
    apiready = function() {
      getbagList();
    }
    var toast = new auiToast({
    })
    function getbagList() {
      var token = localStorage.getItem("token");
      var current_merchant_id = api.pageParam.merchantid;  //扫码支付--跳转到卡券页面（附带的商家id） id-存在：只展示该商家卡券，否则全部显示
        if(!current_merchant_id){
          current_merchant_id == ''
        }
      $.ajax({
        type:"get",
        data:{
          action:1,
          merchid:current_merchant_id
        },
        beforeSend: function(request) {
            request.setRequestHeader("token", token);
        },
        url:dns+"coupon/read",
        success:function(res){

          if(res.count == 0){
            var html = "<p style='text-align:center;font-size:1rem;color:#D39B51;margin:50% auto;'>该商家暂无卡券可领取！</p>";
            document.getElementById("content").innerHTML = html;
            return;
          }
          var html = template("getbag",{"items":res.data});
          document.getElementById("content").innerHTML = html;
          // 领取
          $(".btn_conversion").each(function () {
            $(this).click(function () {
              $(this).css("disabled","disabled")
              $(this).css("background-color","#ccc").css("color","#fff").text("已领取");
              var token = localStorage.getItem("token");
              var coupon_id = $(this).attr("couponId");
              var that = $(this);
              $.ajax({
                type:"post",
                data:{
                  coupon_id:coupon_id,
                  type:1
                },
                beforeSend: function(request) {
                    request.setRequestHeader("token", token);
                },
                url:dns+"coupon/receive",
                success:function(res){
                  // console.log(JSON.stringify(res));
                  that.attr("disabled","disabled")
                  toast.success({
                    title:res.msg,
                    duration:1500
                  });
                },
                error:function(err){
                  var json = JSON.parse(err.responseText)
                  toast.fail({
                    title:json.msg,
                    duration:1500
                  });
                }
              });
            })
          })
        }
      })
    }

</script>

</html>
